package com.shopping.admin.controller.user;

import com.github.pagehelper.Page;
import com.shopping.common.core.object.ResponseResult;
import com.shopping.common.sp.dto.user.UserDto;
import com.shopping.common.sp.query.user.UserQuery;
import com.shopping.common.sp.vo.simple.SimpleVo;
import com.shopping.common.sp.model.user.User;
import com.shopping.service.service.user.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author: zlw
 * @date: 2024/8/15
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加用户
     */
    @PostMapping("/add")
    public ResponseResult<Void> addUser(@RequestBody UserDto dto){
        return userService.addUser(dto);
    }

    /**
     * 分页查询列表
     * @param userQuery
     * @return
     */
    @PostMapping("/page")
    public ResponseResult<HashMap<String, Object>> userPage(@RequestBody UserQuery userQuery){
        return userService.userPage(userQuery);
    }
    /**
     * 通过id获取对象
     * @param userId
     * @return
     */
    @GetMapping("/view")
    public ResponseResult<User> userView(Long userId){
        return userService.userView(userId);
    }

    @PutMapping("/update")
    public ResponseResult<Void> updateUser(@RequestBody UserDto dto){
        return userService.updateUser(dto);
    }

    @DeleteMapping("/delete")
    public ResponseResult<Void> deleteUser(@RequestParam List<Long> userIdList){
        return userService.deleteUser(userIdList);
    }

    @PostMapping("/export")
    public void exportUser(@RequestBody UserQuery userQuery, HttpServletResponse response){
        userService.exportUser(userQuery,response);
    }

    public static void main(String[] args) throws IOException {
//        List<SimpleVo> simpleVoList = new ArrayList<>();
//        SimpleVo simpleVo = new SimpleVo();
//        simpleVo.setSimpleId(1L);
//        simpleVo.setSimpleName("测试");
//        simpleVo.setSimpleDesc("描述");
//        simpleVo.setSimpleStatus(0);
//        simpleVoList.add(simpleVo);
//        System.out.println(simpleVoList instanceof Page);
        String filePath = "D://myProject/export/";
        //写入文件
        File file = new File(filePath + UUID.randomUUID() + ".xlsx");
        FileOutputStream fos = new FileOutputStream(file);
        fos.write("你好".getBytes());
        fos.flush();
    }
}
